<template>
  <div class="comment">
    <el-card>
      <template v-slot:header>
        <my-breadcrumb>评论管理</my-breadcrumb>
      </template>

      <el-table :data="list" style="width: 100%">
        <el-table-column prop="title" label="文章标题" width="180"></el-table-column>
        <el-table-column prop="total_comment_count" label="总评论数" width="180"></el-table-column>
        <el-table-column prop="fans_comment_count" label="粉丝评论数"></el-table-column>
        <el-table-column label="评论状态">
            <template v-slot:default='obj'>
                {{obj.row.comment_status? '关闭' : '开启'}}
            </template>
        </el-table-column>
        <el-table-column label="操作">
            <template v-slot:default='obj'>
                <el-button @click="toggleBtn(obj.row)" v-if="obj.row.comment_status" type="success">开启评论</el-button>
                <el-button @click="toggleBtn(obj.row)" v-else type="danger">关闭评论</el-button>
            </template>
        </el-table-column>
      </el-table>
      <el-pagination
      background
      layout="prev, pager, next"
      @current-change="changePage"
      :total="total_count"
      :current-page="page"
      :page-size="per_page">
    </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { reqGetComments, reqModCommentStatus } from '@/api/comment.js'
import MyBreadcrumb from '@/components/my-breadcrumb.vue'
export default {
  name: 'Commnet',
  components: {
    MyBreadcrumb
  },
  data () {
    return {
      page: 1,
      per_page: 20,
      total_count: 0,
      list: []
    }
  },
  created () {
    this.getComments()
  },
  methods: {
    async getComments () {
      try {
        const res = await reqGetComments(
          this.page,
          this.per_page
        )
        this.list = res.data.data.results
        this.total_count = res.data.data.total_count
      } catch (err) {
        console.log(err)
      }
    },
    changePage (index) {
      this.page = index
      this.getComments()
    },
    async toggleBtn (row) {
      console.log(row.id.toString())
      try {
        await reqModCommentStatus(row.id.toString(), !row.comment_status)
        this.$message.success('修改成功')
        this.getComments()
      } catch (err) {
        console.log(err)
        this.$message.error('修改失败')
      }
    }
  }
}
</script>

<style>
</style>
